package br.com.lopes.gci.controller.Validation;

import java.text.DecimalFormat;
import java.text.ParseException;

import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.component.html.HtmlSelectOneRadio;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.math.NumberUtils;
import org.richfaces.component.html.HtmlCalendar;

import br.com.lopes.gci.util.Constants;
import br.com.lopes.gci.util.MessageUtil;

/**
 * @author Elumini
 * @version 1.0
 */
public abstract class Validator implements IValidator {
	
	protected boolean validateSelectBooleanCheckbox(HtmlSelectBooleanCheckbox selectCheckbox, String message) {
		try {
			if ("false".equals(selectCheckbox.getValue().toString().trim())) {
				MessageUtil.getInstance().setErrorMessageForComponent(message, selectCheckbox
						.getClientId(FacesContext.getCurrentInstance()));
				return false;
			}
		} catch (NullPointerException e) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, selectCheckbox
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}

	protected boolean validateSelectOneRadio(HtmlSelectOneRadio selectOneRadio, String message) {
		try {
			if ("".equals(selectOneRadio.getValue().toString().trim())) {
				MessageUtil.getInstance().setErrorMessageForComponent(message, selectOneRadio
						.getClientId(FacesContext.getCurrentInstance()));
				return false;
			}
		} catch (NullPointerException e) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, selectOneRadio
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}
	
	protected boolean validateInput(HtmlInputText inputText, String message) {
		try {
			if ("".equals(inputText.getValue().toString().trim())) {
				MessageUtil.getInstance().setErrorMessageForComponent(message, inputText
						.getClientId(FacesContext.getCurrentInstance()));
				return false;
			}
		} catch (NullPointerException e) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, inputText
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}
	
	protected boolean validateOutput(HtmlOutputText outputText, String message) {
		try {
			if ("".equals(outputText.getValue().toString().trim())) {
				MessageUtil.getInstance().setErrorMessageForComponent(message, outputText
						.getClientId(FacesContext.getCurrentInstance()));
				return false;
			}
		} catch (NullPointerException e) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, outputText
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}
	
	protected boolean validateCalendar(HtmlCalendar calendar, String message) {
		try {
			if ("".equals(calendar.getValue().toString().trim())) {
				MessageUtil.getInstance().setErrorMessageForComponent(message, calendar
						.getClientId(FacesContext.getCurrentInstance()));
				return false;
			}
		} catch (NullPointerException e) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, calendar
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}
	
	protected boolean validateInputSecret(HtmlInputSecret inputText, String message) {
		try {
			if ("".equals(inputText.getValue().toString().trim())) {
				MessageUtil.getInstance().setErrorMessageForComponent(message, inputText
						.getClientId(FacesContext.getCurrentInstance()));
				return false;
			}
		} catch (NullPointerException e) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, inputText
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}

	protected boolean validateCombo(HtmlSelectOneMenu selectOneMenu,
			String message) {
		if ("-1".equals(selectOneMenu.getValue().toString())) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, selectOneMenu
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}

	protected boolean validateRadio(HtmlSelectOneRadio selectOneRadio,
			String message) {
		if (selectOneRadio.getValue() == null
				|| "".equals(selectOneRadio.getValue().toString())) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, selectOneRadio
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}

	protected boolean validateInputTextArea(HtmlInputTextarea inputTextArea,
			String message) {
		if (inputTextArea.getValue() == null
				|| "".equals(inputTextArea.getValue().toString().trim())) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, inputTextArea
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}

	protected boolean validateIsNumeric(HtmlInputText inputText, String message) {
		if (!"".equals(inputText.getValue().toString().trim())
				&& !NumberUtils.isNumber(inputText.getValue().toString())) {
			MessageUtil.getInstance().setErrorMessageForComponent(message, inputText
					.getClientId(FacesContext.getCurrentInstance()));
			return false;
		}
		return true;
	}
	
	protected boolean validateIsNumericBiggerThanZero(HtmlInputText inputText, String message) {
		try{
			if (!"".equals(inputText.getValue().toString().trim())){
				DecimalFormat moeda = Constants.FORMATADOR_MOEDA;
				double valor = 0;
				try {
					valor = moeda.parse(inputText.getValue().toString()).doubleValue();
				} catch (ParseException e) {}
				if (0d >= valor){
					MessageUtil.getInstance().setErrorMessageForComponent(message, inputText
						.getClientId(FacesContext.getCurrentInstance()));
					return false;
				}
			}
		} catch (NullPointerException e) {
			
		}
		return true;
	}
	
	protected String getErrorMessageFromBundle(String key) {
		return MessageUtil.getInstance().getDefaultMessageFromBundle(key);
	}

	protected void setErrorMessageFromBundle(String key) {
		MessageUtil.getInstance().setDefaultMessageFromBundle(key);
	}
	protected void setInfoMessageFromBundle(String key) {
		MessageUtil.getInstance().setDefaultMessageFromBundle(key);
	}
	
	
	
}